package com.sgb.kjwl.view.ui.adapter.home;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.swgk.core.view.entity.MenuEntity;

import java.util.List;

/**
 * Created by iblade.Wang on 2019/5/22 17:16
 */
public class LabelAdapter extends RecyclerView.Adapter<LabelAdapter.LabelHolder> {
    private List<MenuEntity> list;
    private Activity activity;
    private LayoutInflater inflater;
    private MenuEntity menuEntity;


    public LabelAdapter(List<MenuEntity> list, Activity activity) {
        this.list = list;
        this.activity = activity;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public LabelHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new LabelHolder(inflater.inflate(R.layout.adapter_commodity_conditions_itme, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull LabelHolder holder, int position1) {
        final int position = holder.getAdapterPosition();
        if (list != null && null != list.get(position)) {
            menuEntity = list.get(position);
            holder.textView.setSelected(menuEntity.isSelected());
            holder.textView.setText(menuEntity.getName());
            holder.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onLabelClickListener != null) {
                        onLabelClickListener.onClick(menuEntity, position);
                    }
                }
            });
        }
    }

    public static final int UPDATE_STATE = 101;
    public static final int UPDATE_NAME = 102;

    @Override
    public void onBindViewHolder(@NonNull LabelHolder holder, int position, @NonNull List<Object> payloads) {
        //list为空时，必须调用两个参数的onBindViewHolder(@NonNull LabelHolder holder, int position)
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else if (payloads.get(0) instanceof Integer) {
            int payLoad = (int) payloads.get(0);
            switch (payLoad) {
                case UPDATE_STATE:
                    holder.textView.setSelected(list.get(position).isSelected());
                    break;
                case UPDATE_NAME:
                    holder.textView.setText(list.get(position).getName());
                    break;
                default:
                    break;
            }
        }
    }

    public interface OnLabelClickListener {
        /**
         * 点击label
         *
         * @param bean     点击label的对象
         * @param position 点击位置
         */
        void onClick(MenuEntity bean, int position);
    }

    private OnLabelClickListener onLabelClickListener;

    public void setOnLabelClickListener(OnLabelClickListener onLabelClickListener) {
        this.onLabelClickListener = onLabelClickListener;
    }

    @Override
    public int getItemCount() {
        return null == list ? 0 : list.size();
    }

    final class LabelHolder extends RecyclerView.ViewHolder {
        private TextView textView;

        public LabelHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_type);
        }
    }
}